package com.glorysoft.xztpda.ui.datacollection

import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseViewModel
import com.glorysoft.xztpda.bindingadapter.DataCollectionAdapter
import com.glorysoft.xztpda.entity.EdcDataEDCSETLISTEDCSETBINLINELIST
import com.glorysoft.xztpda.entity.EdcDataEntity
import com.glorysoft.xztpda.entity.LotInfoEntity
import com.glorysoft.xztpda.model.LotRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: CollectionDataViewModel
 * Author: XJH
 * Date: 2022/4/13 16:44
 * Description:
 * History:
 */
class CollectionDataViewModel : BaseViewModel() {

    private val repository: LotRepository = LotRepository()
    var lotInfoEntity: LotInfoEntity? = null
    var edcData: EdcDataEntity? = null
    val edcItemClick = object : DataCollectionAdapter.OnEditClickListener {

        override fun onEditClick(position: Int) {
            defUI.callObserve.value = CallObserveBean(1001,position)
        }

    }

    val dataAdapter = DataCollectionAdapter(null,edcItemClick)



    var edcDataItems = ObservableArrayList<EdcDataEDCSETLISTEDCSETBINLINELIST>()
    var edcDataBinding =
        ItemBinding.of<EdcDataEDCSETLISTEDCSETBINLINELIST>(
            BR.edcItem,
            R.layout.item_edc_data
        )

    fun getLotInfo(lot: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOTINFOBYIDTRACKOUT")
        map.addProperty("LOTID", lot)
        map.addProperty("EQUIPMENTID", "")
        val edcMap = JsonObject()
        edcMap.addProperty("LOTID", lot)
        launchOnlyResult({
            lotInfoEntity = repository.getLotInfo(map)
            edcData = repository.getEdcData(edcMap)
            if(edcData!!.EDCSETLIST.isNotEmpty()){
                edcDataItems.clear()
                edcData!!.EDCSETLIST[0].EDCSETBINLINELIST.forEach{
                    it.MAINQTY = lotInfoEntity!!.lot.mainqty
                }
                edcDataItems.addAll(edcData!!.EDCSETLIST[0].EDCSETBINLINELIST)
                dataAdapter.setNewData(edcData!!.EDCSETLIST[0].EDCSETBINLINELIST)
                defUI.callObserve.value = CallObserveBean(1002,"")
            }else{
                defUI.toastEvent.value = ToastMessageBean("服务器返回EDC数据为空！",false)
            }

        })
    }

    fun submit() {
//        var i = 0
//        edcDataItems.forEach {
//            if (it.VALUE.isNotEmpty()){
//                val toInt = it.VALUE.toInt()
//                i += toInt
//            }
//        }
//        if (i > lotInfoEntity!!.lot.mainqty){

//            defUI.toastEvent.value = ToastMessageBean("列表总数量不能大于批次数量",false)
//            return
//        }
        val map = JsonObject()
        map.addProperty("LOTID", lotInfoEntity!!.lot.lotid)
        edcData!!.EDCSETLIST[0].EDCSETBINLINELIST = edcDataItems
        map.add("EDCSETLIST", JsonParser().parse(Gson().toJson(edcData!!.EDCSETLIST)).asJsonArray)
        launchOnlyResult({
                repository.reportEdcData(map)
            defUI.startActivity.call()
        })
    }
}